package cn.sx.yys.blog.controller;

import cn.sx.yys.blog.entity.Article;
import cn.sx.yys.blog.service.ArticleService;
import cn.sx.yys.support.RequestResult;
import cn.sx.yys.utils.FastdfsUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Article)表控制层
 *
 * @author makejava
 * @since 2021-03-08 10:25:46
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    /**
     * 服务对象
     */
    @Resource
    private ArticleService articleService;

    /**
     * 查询单条文章信息
     * @param articleId
     * @return
     */
    @GetMapping("getOne")
    public RequestResult getOne(Integer articleId){
        //获取文章信息
        Article article = articleService.queryById(articleId);
        //阅读量+1
        article.setArticleGlance(article.getArticleGlance()+1);
        articleService.update(article);
        RequestResult result = RequestResult.success();
        result.appendData("article",article);
        return result;
    }

    /**
     * 分页搜索
     * @param keywords
     * @return
     */
    @GetMapping("search")
    public RequestResult search(
            @RequestParam(name = "pageNumber", defaultValue = "1") Integer pageNumber,
            @RequestParam(name = "pageSize", defaultValue = "15") Integer pageSize,
            String keywords
    ){
        keywords = keywords!=null?keywords:"";
        PageInfo pageInfo = articleService.queryByLike(keywords, pageNumber, pageSize);
        RequestResult result = RequestResult.success();
        result.appendData("page",pageInfo);
        return result;
    }

    /**
     * 按时间分组获取所有文章
     * @return
     */
    @GetMapping("getTimeline")
    public RequestResult getTimeline(){
        List<Map<String, Object>> list = articleService.queryAllGroupByCreateTime();
        RequestResult result = RequestResult.success();
        result.appendData("timelines",list);
        return result;
    }

    /**
     * 分页查询技术专栏或者程序人生文章
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("getTechnicalOrLife")
    public RequestResult getTechnicalOrLife(
            @RequestParam(name = "pageNumber", defaultValue = "1") Integer pageNumber,
            @RequestParam(name = "pageSize", defaultValue = "15") Integer pageSize,
            @RequestParam(name = "articleType", defaultValue = "1") Integer articleType
    ){
        PageInfo pageInfo = articleService.queryTechnicalOrLifeByLimit(pageNumber, pageSize, articleType);
        RequestResult result = RequestResult.success();
        result.appendData("page",pageInfo);
        return result;
    }

    /**
     * 分页查询首页推荐文章
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("getIndexArticle")
    public RequestResult getIndexArticle(
            @RequestParam(name = "pageNumber", defaultValue = "1") Integer pageNumber,
            @RequestParam(name = "pageSize", defaultValue = "15") Integer pageSize
    ){
        PageInfo pageInfo = articleService.queryIndexAllByLimit(pageNumber, pageSize);
        RequestResult result = RequestResult.success();
        result.appendData("page",pageInfo);
        return result;
    }
}
